﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Resources;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using VisualPureBasicIDE.CoreGUI.Properties;

namespace VisualPureBasicIDE.CoreGUI
{
    public enum ApplicationState
    {
        StartingUp,
        Busy,
        Ready
    }

    class ApplicationManager
    {
        static ApplicationManager()
        {
            // Initialize ResourceManager
            ResourceManager = new ResourceManager("VisualPureBasicIDE.CoreGUI.language", typeof(MainForm).Assembly);

            foreach (var culture in CultureInfo.GetCultures(CultureTypes.FrameworkCultures))
            {
                if (!culture.EnglishName.Equals(Settings.Default.PreferredCulture)) continue;

                CultureInfo = culture;
                break;
            }

            if (CultureInfo == null) CultureInfo = new CultureInfo(""); // Invariant language
            Thread.CurrentThread.CurrentUICulture = CultureInfo;
        }

        public static MainForm MainForm
        {
            get 
            {
                return InstanceManager.GetInstance(typeof (MainForm)) as MainForm;
            }
        }

        public static ResourceManager ResourceManager { get; set; }
        public static CultureInfo CultureInfo { get; set; }
        public static ApplicationState ApplicationState { get; set; }

        internal static void ExitAndElevate()
        {
            if (Environment.OSVersion.Platform != PlatformID.Win32NT) return;

            if (Environment.OSVersion.Version.Major >= 6) // Elevating currently supported by Windows Vista and Windows Seven
            {
                var proc = new Process
                {
                    StartInfo = { FileName = Application.ExecutablePath, Verb = "runas" }
                };
                try
                {
                    proc.Start();
                    Application.Exit();
                }
                catch (Win32Exception)
                {
                    //MessageBox.Show("It seems that you canceled the UAC dialog", "UAC dialog canceled");
                }
            }
            else
                MessageBox.Show("You cannot elevate this application, because this OS is incompatible with elevating", "Your OS is incompatible", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        /// <summary>
        /// (Tries to) set the working set of this application to the given values
        /// </summary>
        public static void SetWorkingSet(int maxSize, int minSize)
        {
            if (!InvariantStrings.AdministratorRights) return;

            var loProcess = Process.GetCurrentProcess();
            loProcess.MaxWorkingSet = (IntPtr)maxSize;
            loProcess.MinWorkingSet = (IntPtr)minSize;
        }

        /// <summary>
        /// Reduces the working set using standard values
        /// </summary>
        public static void SetWorkingSet()
        {
            if (!Settings.Default.FreeMemory) return;

            // Reduce our working set to save memory
            SetWorkingSet(750000, 300000);
        }
    }
}